package com.gexin.model.order;

import com.gexin.common.utils.SQLUtils;
import com.gexin.model.order.base.BaseOrderMaster;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * 业务名称:  t_order_master(订单主表) 
 */
@SuppressWarnings("serial")
public class OrderMaster extends BaseOrderMaster<OrderMaster> {
	public static final OrderMaster dao = new OrderMaster();

	public Page<OrderMaster> query(int pageNumber,int pageSize,OrderMaster order){
		List<Object> list =new LinkedList<Object>();
		SQLUtils sql=new SQLUtils(" from t_order_master o left join t_weixin_user u on u.id = o.customer_id  where 1=1  ");
		if(order!=null){
			if(order.getOrderId()!=null){
				sql.append(" and o.order_id like ? ");
				list.add("%"+order.getOrderId()+"%");
			}
			if(order.getOrderMoney()!=null){
				sql.append(" and o.order_money = ? ");
				list.add(order.getOrderMoney());
			}
			if(order.getOrderStatus()!=null){
				sql.append(" and o.order_status = ? ");
				list.add(order.getOrderStatus());
			}
			if(order.getIsPresell()!=null){
				sql.append(" and o.is_presell = ? ");
				list.add(order.getIsPresell());
			}
			if(!StringUtils.isEmpty(order.getShippingUser())){
				sql.append(" and ( o.shipping_user like ? or o.shipping_tel like ? ) ");
				list.add("%"+order.getShippingUser()+"%");
				list.add("%"+order.getShippingUser()+"%");
			}
			if(!StringUtils.isEmpty(order.getStr("start_time")) && !StringUtils.isEmpty(order.getStr("end_time"))){
				sql.append(" and o.create_time BETWEEN ? and ? ");
				list.add(order.getStr("start_time"));
				list.add(order.getStr("end_time"));
			}
			if(order.getCustomerId()!=null){
				sql.append(" and o.customer_id = ? ");
				list.add(order.getCustomerId());
			}

		}
		sql.append(" order by o.create_time desc ");
		SQLUtils select_sql =new SQLUtils("select o.*,u.nickname,u.headimgurl ");
		select_sql.append(",(select tmp.busi_name from t_admin_dict tmp where tmp.busi_value=o.`status` and tmp.parent_id=(select id from t_admin_dict where busi_value='ORDER_STATUS')) as order_status_name ");
		Object[] obj = new Object[0];
		if(list.size()!=0){
			obj = list.toArray();
		}
		return paginate(pageNumber,pageSize,select_sql.toString(),sql.toString(),obj);
	}


	//根据user_id查找他所有的订单
	public List<OrderMaster> findByUserAll(Integer user_id){
		return dao.find("select * from t_order_master where customer_id = ? order by create_time desc  ",user_id);
	}

}
